import numpy as np
from sklearn.preprocessing import StandardScaler

lines = np.loadtxt('../data/USA_Housing.csv', delimiter=',', dtype='str')
print(lines.shape)
header = lines[0]
lines = lines[1:].astype(float)

# 训练集和测试集
ratio = 0.8
split = int(len(lines) * ratio)
lines = np.random.permutation(lines)
train, test = lines[:split], lines[split:]

# 标准化  1 2 3 4 5 均值：3 方差：q
# 均值为0，方差为1的数据 -1.414 -0.707 0 0.707 1.414

scaler = StandardScaler()
scaler.fit(train)
train = scaler.transform(train)
test = scaler.transform(test)

x_train, y_train = train[:,:-1], train[:,-1]
x_test, y_test = test[:,:-1], test[:,-1]

X = np.concatenate([x_train, np.ones((len(x_train), 1))], axis=-1)
theta = np.linalg.inv(X.T @ X) @ X.T @ y_train
print(theta)

X_test = np.concatenate([x_test, np.ones((len(x_test), 1))], axis=-1)
Y_test = X_test @ theta
print(Y_test)

